package com.sise.parkinglot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sise.parkinglot.bean.CodeMsg;
import com.sise.parkinglot.bean.ParkBean;
import com.sise.parkinglot.composition.PublicService;
import com.sise.parkinglot.entity.Park;
import com.sise.parkinglot.mapper.ParkMapper;
import com.sise.parkinglot.service.IParkService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.Serializable;
import java.math.BigDecimal;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author ZJ
 */
@Service
public class ParkServiceImpl extends ServiceImpl<ParkMapper, Park> implements IParkService {

    @Resource
    private ParkMapper parkMapper;

    @Resource
    private IParkService parkService;

    @Resource
    private PublicService publicService;

    @Override
    public CodeMsg<List<Park>> queryParkList(Map<String, Object> map) {
        if (map.isEmpty()) {
            return new CodeMsg<List<Park>>().setCode(400).setMsg("参数缺失");
        }
        Page<Park> page = new Page<>((int) map.get("current"), (int) map.get("pageSize"));
        QueryWrapper<Park> ParkQueryWrapper = new QueryWrapper<>();
        if (map.get("parkId") != null && !map.get("parkId").equals("")) {
            ParkQueryWrapper.eq("park_id", map.get("parkId"));
        }
        if (map.get("carId") != null && !map.get("carId").equals("")) {
            ParkQueryWrapper.eq("car_id", map.get("carId"));
        }
        if (map.get("stationId") != null && !map.get("stationId").equals("")) {
            ParkQueryWrapper.eq("station_id", map.get("stationId"));
        }
        if (map.get("stationType") != null && !map.get("stationType").equals("")) {
            ParkQueryWrapper.eq("station_type", map.get("stationType"));
        }
        if (map.get("carNumber") != null && !map.get("carNumber").equals("")) {
            ParkQueryWrapper.like("car_number", map.get("carNumber"));
        }
        if (map.get("cardType") != null && !map.get("cardType").equals("")) {
            ParkQueryWrapper.eq("card_type", map.get("cardType"));
        }
        if (map.get("startPark") != null && !map.get("startPark").equals("")) {
            String startPark = (String) map.get("startPark");
            ParkQueryWrapper.lambda().likeRight(Park::getStartPark, startPark.split(" ")[0]);
        }
        if (map.get("endPark") != null && !map.get("endPark").equals("")) {
            String endPark = (String) map.get("endPark");
            ParkQueryWrapper.lambda().likeRight(Park::getEndPark, endPark.split(" ")[0]);
        }
        if (map.get("fee") != null && !map.get("fee").equals("")) {
            ParkQueryWrapper.like("fee", map.get("fee"));
        }
        if (map.get("sumPark") != null && !map.get("sumPark").equals("")) {
            ParkQueryWrapper.eq("sum_park", map.get("sumPark"));
        }
        if (map.get("state") != null && !map.get("state").equals("")) {
            ParkQueryWrapper.eq("state", map.get("state"));
        }
        ParkQueryWrapper.lambda().orderByDesc(Park::getStartPark);
        IPage<Park> parkPage = parkMapper.selectPage(page, ParkQueryWrapper);
        List<Park> parkList = parkPage.getRecords();
        return new CodeMsg<List<Park>>().setCode(200).setMsg(String.valueOf(parkPage.getTotal())).setData(parkList);
    }

    @Override
    public CodeMsg<List<ParkBean>> getParkByToken(Map<String, String> map) {
        if (map.isEmpty()) {
            return new CodeMsg<List<ParkBean>>().setCode(400).setMsg("参数缺失");
        }
        String id = publicService.getUserIdByToken(map.get("token"));
        int current = 1;
        int pageSize = 10;
        int state = 3;
        if (map.get("current") != null) {
            current = Integer.parseInt(map.get("current"));
        }
        if (map.get("pageSize") != null) {
            pageSize = Integer.parseInt(map.get("pageSize"));
        }
        if (map.get("state") != null) {
            state = Integer.parseInt(map.get("state"));
        }
        Page<Park> page = new Page<>(current, pageSize);
        QueryWrapper<Park> ParkQueryWrapper = new QueryWrapper<>();
        ParkQueryWrapper.lambda().eq(Park::getCarId, id).orderByDesc(Park::getStartPark);
        if (state != 3) {
            ParkQueryWrapper.lambda().eq(Park::getState, state);
        }
        IPage<Park> parkPage = parkMapper.selectPage(page, ParkQueryWrapper);
        List<Park> parkList = parkPage.getRecords();
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        List<ParkBean> parkBeanList = new ArrayList<>();
        parkList.forEach(v -> {
            ParkBean parkBean = new ParkBean();
            parkBean.setParkId(v.getParkId())
                    .setCarId(v.getCarId())
                    .setStationId(v.getStationId())
                    .setCarNumber(v.getCarNumber())
                    .setStartPark(dtf.format(v.getStartPark()))
                    .setSumPark(v.getSumPark())
                    .setStationType(v.getStationType())
                    .setState(v.getState());
            if (v.getEndPark() != null) {
                parkBean.setEndPark(dtf.format(v.getEndPark()));
            } else {
                parkBean.setEndPark("未离开");
            }
            if (v.getFee() != null) {
                parkBean.setFee(v.getFee());
            } else {
                parkBean.setFee(BigDecimal.valueOf(0));
            }
            if (v.getCardType() != null) {
                parkBean.setCardType(v.getCardType());
            } else {
                parkBean.setCardType(1);
            }
            parkBeanList.add(parkBean);
        });
        return new CodeMsg<List<ParkBean>>().setCode(200).setMsg("获取成功").setData(parkBeanList);
    }

    @Override
    public CodeMsg<String> statePark(Map<String, Object> map) {
        if (map.isEmpty() || map.get("parkId") == null || map.get("state") == null) {
            return new CodeMsg<String>().setCode(400).setMsg("参数缺失");
        }
        int state = (int) map.get("state");
        List<Park> parkList = new ArrayList<>();
        ArrayList<?> ids = (ArrayList<?>) map.get("parkId");
        for (Object id : ids) {
            parkList.add(new Park().setParkId((Integer) id).setState(state));
        }
        if (parkService.updateBatchById(parkList)) {
            return new CodeMsg<String>().setCode(200).setMsg("操作成功");
        }
        return new CodeMsg<String>().setCode(400).setMsg("操作失败");
    }

    @Override
    public CodeMsg<String> pay(Map<String, Object> map) {
        if (map.get("id") == null) {
            return new CodeMsg<String>().setCode(400).setMsg("参数不存在");
        }
        final Park park = parkMapper.selectById((Serializable) map.get("id"));
        if (park == null) {
            return new CodeMsg<String>().setCode(401).setMsg("订单不存在");
        }
        if (park.getState() == 2) {
            return new CodeMsg<String>().setCode(200).setMsg("已支付");
        }
        return new CodeMsg<String>().setCode(400).setMsg("未支付");
    }

    @Override
    public String notifyPay(String order_no, int pay_type, float realmoney, String result, String xddpay_order, String sign) {
        final Park park = parkMapper.selectById(order_no);
        if (park == null) {
            return "fail";
        }
        park.setXddpayOrder(xddpay_order).setPayType(pay_type).setRealmoney(realmoney).setResult(result).setState(2);
        parkMapper.updateById(park);
        return "success";
    }
}
